package ULHT.LIG2N1.RC.SERVER;

import ULHT.LIG2N1.RC.VO.FileManage;
import java.io.IOException;
import java.net.*;

public class ServerThread extends Thread {

    private static int TCPport = 6500, maxConnections = 0, UDPport = 9031, UDPmulticastPort = 9013;
    private static String MulticastGroupIP = "230.0.0.1";
    private static InetAddress group;
    private static FileManage filemanager;

    public ServerThread() {
    }
    
    @Override
    public void run() {
        GameVariables gameVariables = new GameVariables();
        int i = 0;
        System.out.println("Servidor a iniciar...\n\nA aguardar ligação do cliente");
        try {
            ServerSocket TCPsocket = new ServerSocket(TCPport);
            DatagramSocket unicastSocket = new DatagramSocket(UDPport);
            MulticastSocket multicastSocket = new MulticastSocket(UDPmulticastPort);
            group = InetAddress.getByName(MulticastGroupIP);
            Socket TCPClient;

            GameThread threadJogo = new GameThread(multicastSocket, group, UDPmulticastPort, gameVariables);
            Thread jogo = new Thread(threadJogo);
            jogo.start();

            while ((i++ < maxConnections) || (maxConnections == 0)) {
                TCPClient = TCPsocket.accept();
                ClientThread ThreadCliente = new ClientThread(TCPClient, gameVariables);
                Thread t = new Thread(ThreadCliente);
                t.start();
            }
        } catch (IOException ioe) {
            System.out.println("IOException on socket listen: " + ioe);
            ioe.printStackTrace();
        }
    }
}
